package com.tbit.main.config.mvc;

import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.tbit.main.controller.interceptor.LoginInterceptor;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import java.util.ArrayList;
import java.util.List;

/**
 * @author chenzhuang
 * @Description:
 * @date 2022/7/29 17:45
 */
@Configuration
public class MvcConfig implements WebMvcConfigurer {

    /**
     * 注册自定义的LocaleResolver
     **/
    @Bean
    public LocaleResolver localeResolver() {
        return new SessionLocaleResolver();
    }

    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters() {
        FastJsonHttpMessageConverter fastJsonConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
//        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
//        fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteDateUseDateFormat);
//        // Date格式设置
//        fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        fastJsonConverter.setFastJsonConfig(fastJsonConfig);

        //3、在convert中添加配置信息
        //处理中文乱码问题
        List<MediaType> fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.valueOf("text/html;charset=UTF-8"));
        fastMediaTypes.add(MediaType.APPLICATION_JSON);
        fastJsonConverter.setSupportedMediaTypes(fastMediaTypes);
        HttpMessageConverter<?> converter = fastJsonConverter;
        return new HttpMessageConverters(converter);
    }


    @Bean
    public LoginInterceptor loginInterceptor() {
        return new LoginInterceptor();
    }

    /**
     * 添加拦截器
     **/
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/accountUser/login.do")
                .excludePathPatterns("/accountUser/loginWithPassword.do")
                .excludePathPatterns("/heartbeat.do")
                .excludePathPatterns("/sms/send.do")
                .excludePathPatterns("/image/add.do")
                .excludePathPatterns("/image/getByImageId.do")
                .excludePathPatterns("/brand/getAll.do")
                .excludePathPatterns("/host/getHost.do")
                .excludePathPatterns("/version/get.do")
                .excludePathPatterns("/weather/new.do")
                .excludePathPatterns("/cuntry/query.do")
                .excludePathPatterns("/linkConfig/get.do")
                .excludePathPatterns("/accountUser/delRoleToken.do")
                .excludePathPatterns("/accountUser/delToken.do")
                .excludePathPatterns("/accountUser/delLookBattery.do")
                .excludePathPatterns("/detentionArea/getAll.do")
                .excludePathPatterns("/terControl/money.do")
                .excludePathPatterns("/dispatchApplyLog/handle.do")
                .excludePathPatterns("/accountDayStat/test.do")
                .excludePathPatterns("/brandConfig/getByName.do")
        ;
        registry.addInterceptor(new LocaleChangeInterceptor());
    }

    /**
     * 严格按照*。do匹配 会影响‘项目内静态资源’的访问
     */
    /*@Bean(name = "dispatcherServletRegistration")
    public DispatcherServletRegistrationBean dispatcherServletRegistration(DispatcherServlet dispatcherServlet,
                                                                           WebMvcProperties webMvcProperties, ObjectProvider<MultipartConfigElement> multipartConfig) {
        DispatcherServletRegistrationBean registration = new DispatcherServletRegistrationBean(dispatcherServlet,
                "*.do");
        registration.setName(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME);
        registration.setLoadOnStartup(webMvcProperties.getServlet().getLoadOnStartup());
        multipartConfig.ifAvailable(registration::setMultipartConfig);
        return registration;
    }*/
}
